package game.MazeRunner;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.widget.ImageButton;
import android.widget.TextView;

public class MazeRunnerActivity extends Activity {

	MazeView mView;
	ImageButton [] button;
	TextView mTextView;
	boolean isHost;
	int hostID;
	int playerID;
	String playerName;
	
	protected Thread td;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
    	int playerNum = 2;
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        setContentView(R.layout.main);
        
        
        final Bundle bundle = this.getIntent().getExtras();
        String[] IPAddress = new String[4];
        for(int i = 0; i < 4 ; i++){
        	IPAddress[i] = "";
        }
        if(bundle.getString("self").equals("host")){
        	playerID = 0;
        	for(int j = 1; j < 4; j++){
        		IPAddress[j] = bundle.getStringArray("clientAddr")[j];
        	}
        }else{
        	playerID = 1;
        	IPAddress[0] = bundle.getString("hostAddr");
        }
        
        hostID = 0;
        /*
        String[] IPAddress = new String[4];
        for(int i=0;i<4;i++)
        	IPAddress[i]="";
        playerID = 1;
        IPAddress[0]="10.10.10.129"; // right
        IPAddress[1]="10.10.10.123"; // left
        IPAddress[2]="";
        IPAddress[3]="";
        System.err.println("HI");
        */
        button = new ImageButton[4];
        button[0] = (ImageButton) findViewById(R.id.upButton);
        button[1] = (ImageButton) findViewById(R.id.downButton);
        button[2] = (ImageButton) findViewById(R.id.leftButton);
        button[3] = (ImageButton) findViewById(R.id.rightButton);
        mTextView = (TextView) findViewById(R.id.text);
        mView = (MazeView) findViewById(R.id.mazeView);
        mView.setIPAddress(IPAddress);
        mView.startUp(hostID,playerID,playerName,playerNum);
        mView.setServerSocket();
        mView.setImageButtons(button);
        mView.setTextView(mTextView);
        
        mView.start();
        td = new Thread(mView);
    	td.start();
    }
}